import { getTagTableApi } from '@/api/cms/tag/tag'
import { TagListParam } from '@/api/cms/tag/TagModel'
import { StatusCode } from '@/http/request'
import { onMounted, reactive, nextTick, ref } from 'vue'

export default function useTagTable() {
    // 定义列表查询的参数
    const listParam = reactive<TagListParam>({
        current_page: 1,
        page_size: 20,
        name: '',
        status: '1',
        total: 0
    })
    // 表格的高度
    const tableHeight = ref(0)
    // 表格数据
    const tagTable = reactive({
        list: []
    })
    // 获取表格数据
    const getTagTable = async () => {
        let res = await getTagTableApi(listParam)
        if (res && res.code == StatusCode.Success) {
            console.log('标签列表：', res.data.list)
            tagTable.list = res.data.list
            listParam.total = res.data.total
        }
    }

    // 页容量改变时触发
    const sizeChange = (size: number) => {
        listParam.page_size = size
        getTagTable()
    }

    // 页数改变时触发
    const currentChange = (page: number) => {
        listParam.current_page = page
        getTagTable()
    }

    // 搜索
    const searchBtn = () => {
        getTagTable()
    }
    // 重置
    const resetBtn = () => {
        listParam.name = ''
        listParam.status = '1'
        getTagTable()
    }

    // 首次加载
    onMounted(() => {
        // console.log('tagList onMounted')
        getTagTable()
        nextTick(() => {
            tableHeight.value = window.innerHeight - 180
        })
    })

    return {
        listParam,
        resetBtn,
        searchBtn,
        tableHeight,
        tagTable,
        getTagTable,
        sizeChange,
        currentChange
    }
}
